#!/bin/bash
UESTUDIO_SRC="/d/vc/UEStudio/src"
BUILD_DIR="/d/vc/UEStudio/build"

echo "🏗️  UEStudio 构建系统启动..."

# 加载配置
source UEStudio.build.config

# 显示构建选项
show_build_options() {
    echo "可用构建目标:"
    for i in "${!TARGET_PLATFORMS[@]}"; do
        echo "  $((i+1)). ${TARGET_PATFORMS[$i]}"
    done
    
    echo ""
    echo "可用界面主题:"
    for i in "${!UI_THEMES[@]}"; do
        echo "  $((i+1)). ${UI_THEMES[$i]}"
    done
}

# 构建函数
build_uestudio() {
    local platform=$1
    local theme=$2
    local build_type=$3
    
    echo "开始构建: $platform | $theme | $build_type"
    
    # 创建构建目录
    local build_path="$BUILD_DIR/$platform/$build_type"
    mkdir -p "$build_path"
    cd "$build_path"
    
    # 配置构建参数
    local cmake_args=(
        "-DCMAKE_BUILD_TYPE=$build_type"
        "-DUESTUDIO_PLATFORM=$platform"
        "-DUESTUDIO_UI_THEME=$theme"
        "-DCMAKE_INSTALL_PREFIX=../install/$platform/$build_type"
    )
    
    # 平台特定配置
    case $platform in
        *windows*)
            cmake_args+=("-DCMAKE_TOOLCHAIN_FILE=windows.toolchain.cmake")
            ;;
        *linux*)
            cmake_args+=("-DCMAKE_TOOLCHAIN_FILE=linux.toolchain.cmake")
            ;;
        *macos*)
            cmake_args+=("-DCMAKE_TOOLCHAIN_FILE=macos.toolchain.cmake")
            ;;
    esac
    
    # 运行CMake配置
    echo "配置CMake: ${cmake_args[@]}"
    cmake "$UESTUDIO_SRC" "${cmake_args[@]}"
    
    # 编译
    echo "开始编译..."
    make -j$(nproc)
    
    if [ $? -eq 0 ]; then
        echo "✅ UEStudio 构建成功: $build_path/UEStudio.exe"
    else
        echo "❌ 构建失败"
        return 1
    fi
}

# 主构建流程
main() {
    echo "请选择构建配置:"
    show_build_options
    
    # 简单构建所有平台（实际应该提供选择界面）
    for platform in "${TARGET_PLATFORMS[@]}"; do
        for build_type in "Release" "Debug"; do
            for theme in "codeblocks-classic" "vc2022-dark"; do
                build_uestudio "$platform" "$theme" "$build_type"
            done
        done
    done
}

main "$@"
